package com.example.oneMachineLock;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.ReentrantLock;

/**
 * create by mwh 2021-12-11
 * reentantlock锁
 **/

@RestController
@RequestMapping("/test2")
public class ReentLock {
    //lock放外面，公用才能共同竞争一把锁
    ReentrantLock lock = new ReentrantLock();
    @GetMapping("/test")
    public void test() throws InterruptedException {
        lock.lock();
        test1();
        test2();
        test3();
        lock.unlock();

    }

    private void test3() {
        System.out.println(Thread.currentThread().getName()+"snc test3");
    }

    private void test2() {
        System.out.println(Thread.currentThread().getName()+"snc test2");
    }

    private void test1() throws InterruptedException {
        System.out.println(Thread.currentThread().getName()+"snc test1");
        Thread.sleep(3000);
    }

}
